package com.freesky.biz.citic.service.impl;

import com.alibaba.fastjson2.JSON;
import com.freesky.biz.citic.domain.TCiticLucky;
import com.freesky.biz.citic.domain.TCiticNew;
import com.freesky.biz.citic.domain.TCiticNewEnroll;
import com.freesky.biz.citic.mapper.CiticLuckyMapper;
import com.freesky.biz.citic.mapper.CiticNewMapper;
import com.freesky.biz.citic.service.ICiticLuckyService;
import com.freesky.biz.citic.service.ICiticNewService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CiticNewImpl implements ICiticNewService {

    @Autowired
    private CiticNewMapper citicNewMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TCiticNew> findList(TCiticNew citicNew) {
        return citicNewMapper.findList(citicNew);
    }

    @Override
    public AjaxResult exportFile(TCiticNew citicNew) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("CITIC_NEW");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(citicNew, new TCiticNew(), ICiticNewService.class,
                "findList", "富贵新客礼",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TCiticNewEnroll> selectEnrollList(TCiticNewEnroll citicNewEnroll) {
        return citicNewMapper.selectEnrollList(citicNewEnroll);
    }

    @Override
    public AjaxResult exportEnrollFile(TCiticNewEnroll citicNewEnroll) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("CITIC_NEW_ENROLL");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(citicNewEnroll, new TCiticNewEnroll(), ICiticNewService.class,
                "selectEnrollList", "报名列表",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public int updateRechargeById(String processed, String id) {
        return citicNewMapper.updateRechargeById(processed,id);
    }

}
